<template>
  <div>
    <div class="connectBox">
      <el-button type="success" size='small' @click="dialogVisible" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table ref="multipleTable" size='mini' :data="connectList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="id" label="ID" align='center' width='80'>
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column prop="name" :label="$t('名称')" align='center'></el-table-column>
      <el-table-column :label="$t('限制方式')" align='center'>
        <el-table-column prop="ip_type" align='center' :label="$t('IP类型')" :formatter="ipTypeShow"></el-table-column>
        <el-table-column prop="src" align='center' :label="$t('源IP')"></el-table-column>
        <el-table-column prop="dst" align='center' :label="$t('目的IP')"></el-table-column>
        <el-table-column prop="dport" align='center' :label="$t('目的端口号')"></el-table-column>
      </el-table-column>
      <el-table-column :label="$t('限制条件')" align='center'>
        <el-table-column prop="paral_type" align='center' :label="$t('并发类型')" :formatter="paralTypeShow"></el-table-column>
        <el-table-column prop="dir_type" align='center' :label="$t('并发方向')" :formatter="dirTypeShow"></el-table-column>
        <el-table-column prop="count" align='center' :label="$t('并发连接数')"></el-table-column>
      </el-table-column>
      <el-table-column align='center' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <connect-dialog v-if="connectDialogVisible" ref="refConnectDialogVisible"></connect-dialog>
  </div>
</template>
<script>
import { getConnectArray, delConnect } from '@/api/safe/connect'
import ConnectDialog from './components/ConnectDialog'
export default {
  name: 'Ddos',
  components: {
    ConnectDialog
  },
  data () {
    return {
      connectDialogVisible: false,
      connectList: [],
      idStr: ''
    }
  },
  created () {
    this.listData()
  },
  methods: {
    ipTypeShow (val) {
      return val.ip_type === 'ipv4' ? 'IPv4' : 'IPv6'
    },
    paralTypeShow (val) {
      return val.paral_type === 'each' ? this.$t('每IP并发') : this.$t('总并发')
    },
    dirTypeShow (val) {
      return val.dir_type === 'SRC' ? this.$t('源方向') : this.$t('目的方向')
    },
    // 多选按钮
    handleSelectionChange (data) {
      var idStr = ''
      for (var item of data) {
        if (idStr === '') {
          idStr = item.id
        } else {
          idStr = idStr + '@#@' + item.id
        }
      }
      this.idStr = idStr
    },
    // 列表
    listData () {
      getConnectArray().then(res => {
        if (res.status === 0) {
          this.connectList = res.info
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.connectDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refConnectDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.idStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delConnect({ id: this.idStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.idStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    }
  }
}
</script>
<style lang="less" scoped>
.connectBox {
  margin-bottom: 10px;
}
</style>
